package com.example.servlet;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;


import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(urlPatterns = "/servletDemo" ,loadOnStartup = 0)
public class ServletDemo  implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("ServletDemo：的init方法执行：初始化！！！");
            try {
                //模拟初始化方法中执行耗时操作，这里使用了线程休眠的方法：休眠5秒钟
                Thread.sleep(1000*5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println("ServletDemo：的init方法执行：初始化完成！！！");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
           System.out.println("ServletDemo：service方法被执行了！！！");
        PrintWriter writer = servletResponse.getWriter();
        writer.println("Hello Servlet~~~~~~");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("ServletDemo： 的destroy方法执行：销毁了！！");
    }
}
